package com.example.sandbox.dto.Enum;

import com.example.sandbox.helper.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author Lierbao
 * @Date 2024/8/28 15:13
 */
@AllArgsConstructor
@Getter
public enum Currency {

    BRL("BRL", "0.0000", 10000, 3),
    INR("INR", "0.0000", 10000, 3),
    CNY("CNY", "0.0000", 10000, 3),
    USDT("USDT", "0.0000", 10000, 3),
    USD("USD", "0.0000", 10000, 3),
    EUR("EUR", "0.0000", 10000, 3),
    COP("COP", "0.0000", 10000, 3),
    DEFAULT("DEFAULT", "0.0000", 10000, 3); // TODO 目前所有币种位数一致,发生变化时需要调整

    private final String currency;

    private final String decimalFormat;

    private final int divide;

    // 展示的时候保存几位小数
    private final int scale;

    public static Currency of(String currencyStr) {
        if (StringUtils.isNotBlank(currencyStr)) {
            for (Currency currencyEnum : Currency.values()) {
                if (currencyEnum.currency.equals(currencyStr.toUpperCase())) {
                    return currencyEnum;
                }
            }
        }
        throw new BizException("illegal Currency");
    }


    /**
     * BRL转BRL_WALLET
     * @param currencyStr
     * @return
     */
    public static String getWalletCode(String currencyStr) {
        return of(currencyStr).getCurrency() + "_WALLET";
    }

    /**
     * BRL_WALLET反转BRL
     * @param walletCode
     * @return
     */
    public static String fromWalletCode(String walletCode) {
        if (walletCode != null && walletCode.endsWith("_WALLET")) {
            String currencyStr = walletCode.substring(0, walletCode.length() - 7); // 去掉_WALLET
            return of(currencyStr).getCurrency();
        }
        throw new BizException("illegal Wallet Code");
    }


}
